package com.example.readfiletest;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.nio.charset.Charset;

import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

	private static final String TAG = MainActivity.class.getSimpleName();
	
	private TextView tv;
	private Button btn;
	private EditText et;
	
	private Button pandian_btn;
	private Button nopandian_btn;
	
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        pandian_btn = (Button) findViewById(R.id.pandian_btn);
        nopandian_btn = (Button) findViewById(R.id.nopandian_btn);
        
        
    	
        Log.i(TAG, Environment.getExternalStorageDirectory().getPath()+File.separator);
        String state = Environment.getExternalStorageState();
	    if (!Environment.MEDIA_MOUNTED.equals(state)){
	        Log.d(TAG, "Error: external storage is unavailable");
	    }
	    if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
	        Log.d(TAG, "Error: external storage is read only.");
	    }
	    
	    Log.i(TAG,state);
        tv = (TextView) findViewById(R.id.tv);
        btn = (Button) findViewById(R.id.btn);
        et = (EditText) findViewById(R.id.et);
        
        btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this, ZichanDetailActivity.class);
				intent.putExtra(ZichanConstants.SEARCHBYBARCODE, et.getText().toString());
				startActivity(intent);
				
			}
		});
        
        
        pandian_btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent it = new Intent(MainActivity.this, ZichanActivity.class);
				it.putExtra(ZichanConstants.PANDIAN_FLG, ZichanConstants.PANDIAN);
				startActivity(it);
			}
		});
        
        
        nopandian_btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this, ZichanActivity.class);
				intent.putExtra(ZichanConstants.PANDIAN_FLG, ZichanConstants.NO_PANDIAN);
				startActivity(intent);
			}
		});
        
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    /*private void readRandom() {
    	byte[] cont = new byte[16];
		RandomAccessFile random = null;
		try {
			random = new RandomAccessFile(Environment.getExternalStorageDirectory().getPath()+File.separator+
					"pandian1.txt","rw");
			
			int num = Integer.parseInt(et.getText().toString());
			Log.i(TAG, "size:"+random.length());
			
			int lineSize = (int) (random.length() / 486);
			
			Log.i(TAG,"lineSize: "+ lineSize);
			
			random.seek(486*num);
			//String a = new String(random.readLine().getBytes("ISO-8859-1"),"GBK");
			
			//Log.i(TAG,a);
			
			//random.seek(486*5000);
			
			int s = random.read(cont, 0, 16);
			//Log.i(TAG,random.readLine());
			random.close();
			System.out.println(new String(cont));
			System.out.println(s);
			
			Log.i(TAG,s+":"+new String(cont));
			
			//tv.setText(s+":"+new String(cont));
			Message msg = handler.obtainMessage();
			Bundle data = new Bundle();
			data.putString("msg", s+":"+new String(cont));
			msg.setData(data);
			msg.sendToTarget();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }*/
    
    private void bufferRead() {
		File file = new File(Environment.getExternalStorageDirectory().getPath()+File.separator+
				"pandian.txt");
		FileInputStream fis;
		try {
			fis = new FileInputStream(file);
			BufferedReader br = new BufferedReader(new InputStreamReader(fis,Charset.forName("GBK")));
			String content = null;
			/*while( (content = br.readLine()) != null) {
				System.out.println(content);
			}*/
			Log.i(TAG, br.readLine());
			br.close();
			fis.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
    
}
